package redora.rdo.client.model;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.types.DSDataFormat;

public class FieldDataSource extends DataSource {

	private static final String JSON_URL = GWT.getModuleBaseURL()
			+ "modelFieldsView.do?action=getFields";

	public FieldDataSource() {
		DataSourceTextField field_name = new DataSourceTextField("field_name",
				"field_name");
		DataSourceTextField field_type = new DataSourceTextField("field_type",
				"field_type");
		DataSourceBooleanField not_null = new DataSourceBooleanField("not_null",
				"not_null");
		DataSourceBooleanField finder = new DataSourceBooleanField("finder", "finder");
		DataSourceBooleanField lazy = new DataSourceBooleanField("lazy", "lazy");
		DataSourceTextField regexp = new DataSourceTextField("regexp", "regexp");
		DataSourceTextField maxlength = new DataSourceTextField("maxlength",
				"maxlength");
		this.setFields(field_name, field_type, not_null, maxlength,
				finder, regexp, lazy);
		this.setDataFormat(DSDataFormat.JSON);
		this.setDataURL(URL.encode(JSON_URL));
	}

	private static FieldDataSource instance = null;

	public static FieldDataSource getInstance() {
		if (instance == null) {
			instance = new FieldDataSource();
		}
		return instance;
	}

	@Override
	protected Object transformRequest(DSRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("objectName", this.getTitle());
		request.setParams(params);
		return super.transformRequest(request);
	}
}
